package com.extension.company.view.main;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.extension.company.admin.R;
import com.extension.company.common.mvp.BaseFragment;
import com.extension.company.common.utils.Utils;
import com.extension.company.contract.cart.FragmentCartContract;
import com.extension.company.model.entity.ShopCarData;
import com.extension.company.presenter.main.FragmentCartPresenter;
import com.extension.company.view.adapter.FragmentCarAdapter;
import com.extension.company.common.widget.PullRefreshRecyclerView;
import com.extension.company.view.home.hotactivity.OnlineWaitForPayActivity;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.wuhenzhizao.sku.use.ProductSkuDialog;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * 功能：购物车
 */
public class FragmentCar extends BaseFragment<FragmentCartContract.View, FragmentCartPresenter>
		implements FragmentCartContract.View {

	@BindView(R.id.car_all_select)
	CheckBox carAllSelect;
	@BindView(R.id.layout_buttom)
	RelativeLayout layoutButtom;
	Unbinder unbinder;
	@BindView(R.id.m_top)
	LinearLayout m_top;
	@BindView(R.id.car_smart)
	SmartRefreshLayout smartRefreshLayout;
	@BindView(R.id.car_recycleview)
	RecyclerView carRecycleView;
	@BindView(R.id.car_total_price)
	TextView totalPriceTv;
	@BindView(R.id.car_buy)
	TextView buyTv;
	private Context context;
	private Activity activity;
	private List<ShopCarData> list;
	private FragmentCarAdapter adapter;
	private double totalPrice;



	@Override
	public void onAttach(Context context) {
		super.onAttach(context);
		this.context = context;
	}

	@Override
	public FragmentCartContract.View createView() {
		return this;
	}

	@Override
	public FragmentCartPresenter createPresenter() {
		return new FragmentCartPresenter();
	}

	@Override
	public void init() {
		activity = getActivity();
		initView();
		initData();
		initFakeData();
	}

	@Override
	public int getMainLayout() {
		return R.layout.fragment_car;
	}

	private void initView() {
		m_top = getViewById(R.id.m_top);
		//如果4.4及以上可以让状态栏透明
		if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT
				&& Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
			LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) m_top.getLayoutParams();
			layoutParams.height = Utils.getStatusBarHeight(activity);
			m_top.setLayoutParams(layoutParams);
			m_top.setBackgroundColor(activity.getResources().getColor(R.color.black2));
		} else {
			LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) m_top.getLayoutParams();
			layoutParams.height = Utils.getStatusBarHeight(activity);
			m_top.setLayoutParams(layoutParams);
			m_top.setBackgroundColor(activity.getResources().getColor(R.color.white));
		}
	}

	private void initData() {
		list = new ArrayList<>();

		carRecycleView.setLayoutManager(new LinearLayoutManager(context));
		adapter = new FragmentCarAdapter(activity, list);
		carRecycleView.setAdapter(adapter);

		adapter.setItemCheckListener(new FragmentCarAdapter.ItemCheckListener() {  // 回调计算总价格
			@Override
			public void onCalculateChange(List<ShopCarData> mData) {
				calculate(mData);
			}
		});
	}

	@Override
	public void showLoading() {

	}

	@Override
	public void hideLoading() {

	}

	@Override
	public void onError(Throwable throwable) {

	}


	/**
	 * 删除，虚拟数据
	 */
	private void initFakeData() {
		presenter.getShopList();
	}

	@Override
	public void onDestroyView() {
		super.onDestroyView();
	}

	@OnClick({R.id.car_all_select})
	public void onViewClicked(View view) {
		switch (view.getId()) {
			case R.id.car_all_select:
				adapter.allSelect(((CheckBox) view).isChecked());
				break;
			default:
				break;
		}
	}

	/**
	 * 功能：计算购物车的总价
	 *
	 * @param shopCarDataList 商品的列表
	 */
	public void calculate(List<ShopCarData> shopCarDataList) {
		totalPrice = 0.00;
		if (null == shopCarDataList) {
			return;
		}
		for (ShopCarData shopCarData : shopCarDataList) {
			if (shopCarData.getItemType() == 1) {

				if (shopCarData.isChecked()) {
					totalPrice += Double.valueOf(shopCarData.getCommodityPrice()) * Double.valueOf(shopCarData.getCommodityNumber());
				}
			}
		}
		totalPriceTv.setText(totalPrice + "");
	}

	@OnClick({R.id.car_buy})
	public void onClick(View view) {
		switch (view.getId()) {
			case R.id.car_buy:
//				presenter.payForCar("34-57-2/53-69_66-2"); // 购物车--购买
				StringBuilder builder = new StringBuilder();
				for (int i = 0; i < list.size(); i++) {
					builder.append(list.get(i).getGoodSkuId()).append("/");
				}
				Log.d("zkw","提交的订单的:"+builder.toString());
				Intent intent = new Intent(context, OnlineWaitForPayActivity.class);
				intent.putExtra("goods_Info_Id", "a53-b69_66-c6/a45-b58-c1");
				startActivity(intent);
				break;
			default:
				break;
		}
	}

	@Override
	public void shopCarSuccess(List<ShopCarData> list) { // 组装好的购物车的数据
		this.list = list;
		adapter.setContent(list);
		adapter.notifyDataSetChanged();
	}
}
